﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Collections;
using Cashier_Utils;
using System.Data.Common;

namespace Cashier_Service
{
    public class BillShipmentGoodsService
    {
        /// <summary>
        /// 查询出货单商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public BillShipmentGoods GetBillShipmentGoods(long id)
        {
            BillShipmentGoods shipmentGoods = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    shipmentGoods = new BillShipmentGoods();
                    shipmentGoods.Id = id;
                    shipmentGoods = new BillShipmentGoodsDAO().GetBillShipmentGoods(conn, shipmentGoods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return shipmentGoods;
        }

        /// <summary>
        /// 查询出货单商品列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="enterpriseNo">企业业务号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="barcode">商品条码</param>
        /// <param name="goodsname">商品名称</param>
        /// <param name="categoryname">分类名称</param>
        /// <param name="shipmentno">出货单号</param>
        /// <param name="adminUsername">出货人</param>
        /// <param name="memberno">客户</param>
        /// <param name="startdate">查询开始日期</param>
        /// <param name="enddate">查询结束日期</param>
        /// <param name="ascfield"></param>
        /// <param name="descfield"></param>
        /// <returns></returns>
        public DataTable GetBillShipmentGoodsTable(int pageindex, int pagesize, String enterpriseNo, String storeNo, String barcode, String goodsname, String categoryname,
            String shipmentno, String adminUsername, String memberno, String startdate, String enddate, String ascfield, String descfield)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillShipmentGoods shipmentGoods = new BillShipmentGoods();
                    shipmentGoods.Pageindex = pageindex;
                    shipmentGoods.Pagesize = pagesize;
                    shipmentGoods.EnterpriseNo = enterpriseNo;
                    shipmentGoods.StoreNo = storeNo;
                    shipmentGoods.Barcode = barcode;
                    shipmentGoods.GoodsName = goodsname;
                    shipmentGoods.CategoryName = categoryname;
                    shipmentGoods.Shipmentno = shipmentno;
                    shipmentGoods.AdminUsername = adminUsername;
                    shipmentGoods.Memberno = memberno;
                    shipmentGoods.StartDate = startdate;
                    shipmentGoods.EndDate = enddate;
                    shipmentGoods.Ascfield = ascfield;
                    shipmentGoods.Descfield = descfield;
                    dt = new BillShipmentGoodsDAO().GetBillShipmentGoodsTable(conn, shipmentGoods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询出货单商品记录数
        /// </summary>
        /// <param name="enterpriseNo">企业业务号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="barcode">商品条码</param>
        /// <param name="goodsname">商品名称</param>
        /// <param name="categoryname">分类名称</param>
        /// <param name="shipmentno">出货单号</param>
        /// <param name="adminUsername">出货人</param>
        /// <param name="memberno">客户</param>
        /// <param name="startdate">查询开始日期</param>
        /// <param name="enddate">查询结束日期</param>
        /// <returns></returns>
        public long GetBillShipmentGoodsRecord(String enterpriseNo, String storeNo, String barcode, String goodsname, String categoryname,
            String shipmentno, String adminUsername, String memberno, String startdate, String enddate)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillShipmentGoods shipmentGoods = new BillShipmentGoods();
                    shipmentGoods.EnterpriseNo = enterpriseNo;
                    shipmentGoods.StoreNo = storeNo;
                    shipmentGoods.Barcode = barcode;
                    shipmentGoods.GoodsName = goodsname;
                    shipmentGoods.CategoryName = categoryname;
                    shipmentGoods.Shipmentno = shipmentno;
                    shipmentGoods.AdminUsername = adminUsername;
                    shipmentGoods.Memberno = memberno;
                    shipmentGoods.StartDate = startdate;
                    shipmentGoods.EndDate = enddate;
                    record = new BillShipmentGoodsDAO().GetBillShipmentGoodsRecord(conn, shipmentGoods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 插入出货单商品
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public int InsertBillShipmentGoods(DbConnection conn, ArrayList list)
        {
            int count = 0;
            try
            {
                if (list != null && list.Count > 0)
                {
                    BillShipmentGoodsDAO shipmentGoodsDAO = new BillShipmentGoodsDAO();
                    GoodsService goodsService = new GoodsService();
                    foreach (BillShipmentGoods lopGoods in list)
                    {
                        long id = shipmentGoodsDAO.InsertBillShipmentGoods(conn, lopGoods);
                        if (id > 0)
                        {
                            count += 1;
                            goodsService.UpdateGoodsStock(conn, lopGoods.GoodsId, lopGoods.Numbers, ActKey.SUB);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }

        /// <summary>
        /// 更新出货商品数量
        /// </summary>
        /// <param name="id"></param>
        /// <param name="numbers"></param>
        /// <returns></returns>
        public int UpdateBillShipmentGoods(long id, int numbers)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillShipmentGoods shipmentGoods = new BillShipmentGoods();
                    shipmentGoods.Id = id;
                    shipmentGoods.Numbers = numbers;
                    flag = new BillShipmentGoodsDAO().UpdateBillShipmentGoods(conn, shipmentGoods);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除出货商品
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteBillshipmentGoods(String ids)
        {
            int count = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    if (!String.IsNullOrEmpty(ids))
                    {
                        conn.Open();
                        String[] arr = ids.Split(new Char[] { ',' });
                        BillShipmentGoodsDAO shipmentGoodsDAO = new BillShipmentGoodsDAO();
                        BillShipmentGoods shipmentGoods = new BillShipmentGoods();
                        foreach (String lopid in arr)
                        {
                            shipmentGoods.Id = Int64.Parse(lopid);
                            count += shipmentGoodsDAO.DeleteBillShipmentGoods(conn, shipmentGoods);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }

        /// <summary>
        /// 删除出货单商品信息
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="shipmentno"></param>
        /// <returns></returns>
        public int DeleteBillshipmentGoods(DbConnection conn, String shipmentno)
        {
            int count = 0;
            try
            {
                BillShipmentGoodsDAO shipmentGoodsDAO = new BillShipmentGoodsDAO();
                BillShipmentGoods shipmentGoods = new BillShipmentGoods();
                shipmentGoods.Shipmentno = shipmentno;
                count += shipmentGoodsDAO.DeleteBillShipmentGoods(conn, shipmentGoods);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }

        /// <summary>
        /// 查询ShipmentGoods对象数组
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="enterpriseNo">企业业务号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="barcode">商品条码</param>
        /// <param name="goodsname">商品名称</param>
        /// <param name="categoryname">分类名称</param>
        /// <param name="shipmentno">出货单号</param>
        /// <param name="adminUsername">出货人</param>
        /// <param name="memberno">客户</param>
        /// <param name="startdate">查询开始日期</param>
        /// <param name="enddate">查询结束日期</param>
        /// <param name="ascfield"></param>
        /// <param name="descfield"></param>
        /// <returns></returns>
        public ArrayList GetShipmentGoodsList(int pageindex, int pagesize, String enterpriseNo, String storeNo, String barcode, String goodsname, String categoryname,
            String shipmentno, String adminUsername, String memberno, String startdate, String enddate, String ascfield, String descfield)
        {
            ArrayList list = null;
            try
            {
                DataTable dt = this.GetBillShipmentGoodsTable(pageindex, pagesize, enterpriseNo, storeNo, barcode, goodsname, categoryname,
                    shipmentno, adminUsername, memberno, startdate, enddate, ascfield, descfield);
                if (dt != null && dt.Rows.Count > 0)
                {
                    list = new ArrayList();
                    BillShipmentGoodsDAO shipmentGoodsDAO = new BillShipmentGoodsDAO();
                    foreach (DataRow row in dt.Rows)
                    {
                        BillShipmentGoods goods = new BillShipmentGoods();
                        shipmentGoodsDAO.DataRowToModel(row, goods);
                        list.Add(goods);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }
    }
}
